package com.hspedu.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author DL5O
 * @version 1.0
 */
public class SocketTCP03Server {
    public static void main(String[] args) throws IOException {
        String charset = "utf-8";
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端，在9999端口监听,等待客户端连接...");
        Socket socket = serverSocket.accept();
        System.out.println("客户端:" + socket.getClass() + " 已连接");


        //接受客户端发来的消息
        InputStream inputStream = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset));
        System.out.println(br.readLine());
        socket.shutdownInput();

        //往服务端发来的消息
        String str = "hello,client";
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream,charset));
        bw.write(str);
        bw.newLine();
        bw.flush();

        bw.close();
        br.close();
        socket.close();
        System.out.println("服务端退出...");
    }
}
